package uilayer;
import ctrlayer.*;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.*;
import java.util.InputMismatchException;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * Textbased UI which allows the user to input information about Ordre.
 * 
 * @author Søren Kaae
 * @version 09.12.2014
 */
public class OrdreUI
{
    private OrdreCtr oCtr;
    private VareCtr vCtr;
    //private PersonCtr pCtr;
    private static OrdreUI instance;

    /**
     * Constructor for objects of class VareUI
     */
    private OrdreUI()
    {
        oCtr = new OrdreCtr();
        vCtr = new VareCtr();
        //pCtr = new PersonCtr();
    }

    /**
     * Singleton Method getInstance
     *
     * Creates an instance of OrdreUI if one does not exist
     * 
     * @return instance of OrdreUI
     */
    public static OrdreUI getInstance()
    {
        if(instance == null)
        {
            instance = new OrdreUI();
        }
        return instance;
    }

    /**
     * Method ordreMenu
     *
     * Checks for the userinput in the writeVareMenu method.
     * Each number results in different methods. 
     */
    public void  ordreMenu()
    {
        boolean exit = false;
        while(!exit)//that is: while exit is false
        {
            int choice = writeVareMenu();
            switch(choice)
            {
                //create ordre
                case 1:
                int i = inputTilbud();
                int j = inputOrdre();
                int iNy = i % 2;
                int jNy = j % 2;
                boolean tilbud = false;
                boolean ordre = false;
                if(iNy == 1)
                {
                    tilbud = true;
                }
                if(jNy == 1 && iNy == 0)
                {
                    ordre = true;
                }
                oCtr.createOrdre(inputTlf(), tilbud, ordre, inputDato(), inputStatus(), 0);
                break;
                //find ordre
                case 2:
                oCtr.findOrdre(inputOrdreNr());
                break;
                // find vare på ordre
                case 3: 
                int ordreNr = inputOrdreNr();
                if(oCtr.findOrdre(ordreNr) == null)
                {
                    System.out.println("Ordren kunne desværre ikke findes. Prøv igen.");
                }
                else
                {
                    oCtr.findVarePåListe(ordreNr, inputVareID());
                }
                break;
                //Tilføj vare til ordre
                case 4:
                int ordreNr1 = inputOrdreNr();
                if(oCtr.findOrdre(ordreNr1) == null)
                {
                    System.out.println("Ordren kunne desværre ikke findes. Prøv igen.");
                }
                else
                {
                    oCtr.addVareTilOrdre(ordreNr1, inputVareID(), inputAntal());
                }
                break;
                //rediger antal af vare
                case 5:
                int ordreNr2 = inputOrdreNr();
                if(oCtr.findOrdre(ordreNr2) == null)
                {
                    System.out.println("Ordren kunne desværre ikke findes. Prøv igen.");
                }
                else
                {
                    oCtr.setAntal(ordreNr2, inputVareID(), inputAntal());
                }
                break;
                //fjern vare fra ordre
                case 6:
                int ordreNr3 = inputOrdreNr();
                if(oCtr.findOrdre(ordreNr3) == null)
                {
                    System.out.println("Ordren kunne desværre ikke findes. Prøv igen.");
                }
                else
                {
                    oCtr.deleteVareFraListe(ordreNr3, inputVareID());
                }
                break;
                //rediger status
                case 7:
                int ordreNr4 = inputOrdreNr();
                if(oCtr.findOrdre(ordreNr4) == null)
                {
                    System.out.println("Ordren kunne desværre ikke findes. Prøv igen.");
                }
                else
                {
                    oCtr.findOrdre(ordreNr4).setStatus(inputStatus());
                }
                break;
                case 8:
                int ordreNr5 = inputOrdreNr();
                if(oCtr.findOrdre(ordreNr5) == null)
                {
                    System.out.println("Ordren kunne desværre ikke findes. Prøv igen.");
                }
                else
                {
                    oCtr.afslutOrdre(ordreNr5);
                }
                break;
                // slet ordre
                case 9:
                oCtr.removeOrdre(inputTlf(), inputOrdreNr());
                break;
                //save
                case 10:
                try
                {
                    oCtr.save();
                }
                catch(IOException e)
                {
                    System.out.println("file not found");
                }
                System.out.println("file saved"); 
                break;
                //load
                case 11:
                try
                {
                    oCtr.load();
                }
                catch(ClassNotFoundException e)
                {
                    System.out.println("file not found");
                }
                catch(IOException e)
                {
                    System.out.println("file not found");
                }
                break;
                //exit
                default:
                exit = true;
            }
        }//end while
    }

    public void loadOrdre()
    {
        try
        {
            oCtr.load();
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Der skete en fejl under indlæsning af ordre");
        }
        catch(IOException e)
        {
            System.out.println("Der skete en fejl under indlæsning af ordre");
        }
        oCtr.ordreNoCounterCheck();
    }

    public void save()
    {
        try
        {
            oCtr.save();
        }
        catch(IOException e)
        {
            System.out.println("Filen kunne ikke gemmes");
        }
        System.out.println("Filen er gemt");
    }

    /**
     * Method writeVareMenu
     * 
     * Allows the user to create, find, update and delete orders.
     * 
     * @return An int based on one of the choices.
     */
    private int writeVareMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("*** Ordre menu ***");
        System.out.println(" (1) Tilføj ordre");
        System.out.println(" (2) Find ordre");
        System.out.println(" (3) Find vare på ordre");
        System.out.println(" (4) Tilføj vare til ordre");
        System.out.println(" (5) Rediger antal af vare på ordre");
        System.out.println(" (6) Fjern vare på ordre");
        System.out.println(" (7) Rediger Status");
        System.out.println(" (8) Afslut ordre");
        System.out.println(" (9) Slet ordre");
        System.out.println(" (10) Save ordre");
        System.out.println(" (11) Load ordre");
        System.out.println(" (12) Back");
        System.out.print("\n Choose: ");
        int choise;
        while(!keyboard.hasNextInt()){
            System.out.println("Indtast venligst et tal!");
            keyboard.next(); 
        }
        choise = keyboard.nextInt();
        System.out.print("\n");
        return choise;
    }

    /**
     * Method inputDato
     *
     * @return A date
     */
    public String inputDato()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Ordrens dato:  ");
        String dato = keyboard.nextLine();
        return dato;
    }

    /**
     * Method inputTlf
     *
     * @return A phonenumber
     */
    public String inputTlf()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Kundens telefonnummer:  ");
        String tlf = keyboard.nextLine();
        return tlf;   
    }

    /**
     * Method inputTilbud
     *
     * @return An int offer
     */
    public int inputTilbud()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Angiv tilbud(int):  ");
        int tilbud = keyboard.nextInt();
        return tilbud;
    }

    /**
     * Method inputOrdre
     *
     * @return An int order
     */
    public int inputOrdre() 

    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Angiv ordre(int):  ");
        int ordre = keyboard.nextInt();
        return ordre;
    }

    /**
     * Method inputStatus
     *
     * @return A status
     */
    public String inputStatus()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Ordrens status:  ");
        String status = keyboard.nextLine();
        return status;
    }

    /**
     * Method inputOrdreNr
     *
     * @return An ordernumber
     */
    public int inputOrdreNr()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Angiv ordrens nummer:  ");
        int ordreNr = keyboard.nextInt();
        return ordreNr;
    }

    /**
     * Method inputVareID
     *
     * @return A ware ID
     */
    public int inputVareID()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Varens ID:  ");
        int id = keyboard.nextInt();
        return id;
    }

    /**
     * Method inputAntal
     *
     * @return The number of wished wares
     */
    public int inputAntal()
    {
        Scanner keyboard = new Scanner(System.in);  
        System.out.println(" Antal på ønsket køb:  ");
        int antal = keyboard.nextInt();
        return antal;
    }
}
